{% extends 'include/base.html' %}
{% block title %}Новая запись{% endblock %}
{% block content %}

{% load user_filters %}

<div class='row justify-content-center'>
  <div class='col-md-8 p-5'>
    <div class='card'>
      <div class='card-header'>{% if post %}Редактировать{% else %}Добавить{% endif %} запись</div>
      <div class='card-body'>
        
        {% for error in form.errors %}
          <div class='alert alert-danger' role='alert'>
            {{ error|escape }}
          </div>
        {% endfor %}

        <form method='post' enctype="multipart/form-data" action='{% if post %}{% url "post_edit" username=post.author.username post_id=post.id %}{% else %}{% url "new_post" %}{% endif %}'>
          {% csrf_token %}

          {% for field in form %}
            <div class='form-group row' aria-required={% if field.field.required %}'true'{% else %}'false'{% endif %}>
                <label for='{{ field.id_for_label }}' class='col-md-4 col-form-label text-md-right'>{{ field.label }}{% if field.field.required %}<span class='required'>*</span>{% endif %}</label>
                <div class='col-md-6'>

                  {{ field|addclass:'form-control' }}

                  {% if field.help_text %}
                  <small id='{{ field.id_for_label }}-help' class='form-text text-muted'>{{ field.help_text|safe }}</small>
                  {% endif %}
                </div>        
            </div>
          {% endfor %}

          <div class='col-md-6 offset-md-4'>        
              <button type='submit' class='btn btn-primary'>
                {% if post %}Сохранить{% else %}Добавить{% endif %}
              </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

{% endblock %}